package com.atguigu.day08;

import com.atguigu.utils.OrderEvent;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

public class Example1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<OrderEvent> buyStream = env
                .fromElements(
                        new OrderEvent(
                                "order-1",
                                "buy",
                                60 * 60 * 1000L
                        ),
                        new OrderEvent(
                                "order-1",
                                "buy",
                                120 * 60 * 1000L
                        )
                )
                .assignTimestampsAndWatermarks(
                        // 设置最大延迟时间为0
                        WatermarkStrategy.<OrderEvent>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<OrderEvent>() {
                                    @Override
                                    public long extractTimestamp(OrderEvent element, long recordTimestamp) {
                                        return element.ts;
                                    }
                                })
                );

        SingleOutputStreamOperator<OrderEvent> pvStream = env
                .fromElements(
                        new OrderEvent("order-1", "pv", 10 * 60 * 1000L),
                        new OrderEvent("order-1", "pv", 20 * 60 * 1000L),
                        new OrderEvent("order-1", "pv", 30 * 60 * 1000L),
                        new OrderEvent("order-1", "pv", 70 * 60 * 1000L),
                        new OrderEvent("order-1", "pv", 80 * 60 * 1000L),
                        new OrderEvent("order-1", "pv", 140 * 60 * 1000L)
                )
                .assignTimestampsAndWatermarks(
                        // 设置最大延迟时间为0
                        WatermarkStrategy.<OrderEvent>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<OrderEvent>() {
                                    @Override
                                    public long extractTimestamp(OrderEvent element, long recordTimestamp) {
                                        return element.ts;
                                    }
                                })
                );

        buyStream.keyBy(r -> r.orderId)
                .intervalJoin(pvStream.keyBy(r -> r.orderId))
                .between(Time.hours(-1), Time.hours(1))
                .process(new ProcessJoinFunction<OrderEvent, OrderEvent, String>() {
                    @Override
                    public void processElement(OrderEvent left, OrderEvent right, Context ctx, Collector<String> out) throws Exception {
                        out.collect(left + " => " + right);
                    }
                })
                .print();

        env.execute();
    }
}
